package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.guesstv.lib.module.util.GtPrefUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityGame2Binding;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.view.container.StkRelativeLayout;

public class Game2Activity extends BaseAc<ActivityGame2Binding> {

    //看图猜角色
    private int level = 1;
    private List<TvActorBean> listGame;
    private List<String> listAnswer = new ArrayList<>();
    private int rightNum;

    @Override
    protected int onCreate() {
        return R.layout.activity_game2;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivGame2Back.setOnClickListener(v -> onBackPressed());

        mDataBinding.rlGame2OptionA.setOnClickListener(this);
        mDataBinding.rlGame2OptionB.setOnClickListener(this);
        mDataBinding.rlGame2OptionC.setOnClickListener(this);
        mDataBinding.rlGame2OptionD.setOnClickListener(this);
        mDataBinding.ivGame2Next.setOnClickListener(this);
        mDataBinding.ivGame2Tip.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.rlGame2OptionA://选项A
                isRight(1, mDataBinding.rlGame2OptionA, mDataBinding.tvGame2OptionA.getText().toString());
                break;
            case R.id.rlGame2OptionB://选项B
                isRight(2, mDataBinding.rlGame2OptionB, mDataBinding.tvGame2OptionB.getText().toString());
                break;
            case R.id.rlGame2OptionC://选项C
                isRight(3, mDataBinding.rlGame2OptionC, mDataBinding.tvGame2OptionC.getText().toString());
                break;
            case R.id.rlGame2OptionD://选项D
                isRight(4, mDataBinding.rlGame2OptionD, mDataBinding.tvGame2OptionD.getText().toString());
                break;
            case R.id.ivGame2Next://下一关
                if (level == listAnswer.size() - 1) {
                    ToastUtils.showShort(R.string.it_is_already_the_last_level);
                    return;
                }
                clearView();
                level++;
                setData(listGame.get(level));
                break;
            case R.id.ivGame2Tip://提示
                tip();
                break;
        }
    }

    @Override
    protected void initData() {
        listGame = GtDataProvider.getActorData();
        if (listGame == null || listGame.size() == 0) return;
        for (TvActorBean bean : listGame) {
            listAnswer.add(bean.name);
        }
        level = GtPrefUtil.getActorPass() - 1;
        setData(listGame.get(level));
    }

    //设置游戏数据
    private void setData(TvActorBean bean) {
        mDataBinding.ivGame2Next.setVisibility(View.GONE);
        mDataBinding.ivGame2Img.setImageBitmap(GtDataProvider.getGuessImgBitmap(bean.imgName));//图片
        List<String> list = RandomUtil.randomGetItems(listAnswer, bean.name, 3);
        mDataBinding.tvGame2OptionA.setText(list.get(0));
        mDataBinding.tvGame2OptionB.setText(list.get(1));
        mDataBinding.tvGame2OptionC.setText(list.get(2));
        mDataBinding.tvGame2OptionD.setText(list.get(3));
        boolean isA = checkAnswer(list.get(0), bean.name);
        boolean isB = checkAnswer(list.get(1), bean.name);
        boolean isC = checkAnswer(list.get(2), bean.name);
        boolean isD = checkAnswer(list.get(3), bean.name);
        if (isA) {
            rightNum = 1;
        }
        if (isB) {
            rightNum = 2;
        }
        if (isC) {
            rightNum = 3;
        }
        if (isD) {
            rightNum = 4;
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }


    //提示
    private void tip() {
        switch (rightNum) {
            case 1:
                isRight(rightNum, mDataBinding.rlGame2OptionA, mDataBinding.tvGame2OptionA.getText().toString());
                break;
            case 2:
                isRight(rightNum, mDataBinding.rlGame2OptionB, mDataBinding.tvGame2OptionB.getText().toString());
                break;
            case 3:
                isRight(rightNum, mDataBinding.rlGame2OptionC, mDataBinding.tvGame2OptionC.getText().toString());
                break;
            case 4:
                isRight(rightNum, mDataBinding.rlGame2OptionD, mDataBinding.tvGame2OptionD.getText().toString());
                break;
        }
    }

    //判断对错
    private void isRight(int kind, StkRelativeLayout viewBg, String rightAnswer) {
        if (kind == rightNum) {
            viewBg.setBackgroundColor(Color.parseColor("#4DFF61"));
            //判断是否需要解锁下一关
            if (level < (listGame.size() - 1)) {//如果当前关卡小于总关卡数
                int spLevel = GtPrefUtil.getActorPass();//获取记录通过关卡数
                if (level == (spLevel - 1)) {//如果通过的是未解锁关卡则解锁下一关
                    GtPrefUtil.saveActorPass(spLevel + 1);
                }
            }
//            if (level == (listGame.size() - 1)) {
//                ToastUtils.showShort(R.string.it_is_already_the_last_level);
//                setData(listGame.get(level));
//                return;
//            }
            ToastUtils.showShort(getString(R.string.answer_right));
            mDataBinding.ivGame2Next.setVisibility(View.VISIBLE);
        } else {
            viewBg.setBackgroundColor(Color.parseColor("#FF4D4D"));
            ToastUtils.showShort(getString(R.string.answer_error));
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewBg.setBackgroundColor(Color.parseColor("#271F0C"));
                }
            }, 500);
        }
    }

    private void clearView() {
        mDataBinding.rlGame2OptionA.setBackgroundColor(Color.parseColor("#271F0C"));
        mDataBinding.rlGame2OptionB.setBackgroundColor(Color.parseColor("#271F0C"));
        mDataBinding.rlGame2OptionC.setBackgroundColor(Color.parseColor("#271F0C"));
        mDataBinding.rlGame2OptionD.setBackgroundColor(Color.parseColor("#271F0C"));
    }

}